package chenji.dailykit.ItemEditor.operationnode.nodes;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import chenji.dailykit.Main;
import chenji.dailykit.ItemEditor.ItemEditor;
import chenji.dailykit.ItemEditor.operationnode.OperationNode;
import chenji.dailykit.ItemEditor.operationnode.SimpleItemStack;

public class LoreEditor extends OperationNode {
	/**
	 * 1 - 等待添加lore模式。 2 - 等待编辑行数模式。 3 - 等待编辑内容模式。 4 - 等待删除行数模式
	 */
	int state;
	int temp;

	public LoreEditor(ItemStack i, ItemEditor e, Player p) {
		super(i, e, p);

	}

	@Override
	public void setInventory(Inventory inv) {
		inv.setItem(28, new SimpleItemStack(Material.ENCHANTED_BOOK, new String[] { "§a列出lore" }, " "));
		inv.setItem(29, new SimpleItemStack(Material.BOOK, new String[] { "§a增加一行lore" }, " "));
		inv.setItem(30, new SimpleItemStack(Material.BOOK, new String[] { "§a编辑一行lore" }, " "));
		inv.setItem(31, new SimpleItemStack(Material.BOOK, new String[] { "§a删除一行lore" }, " "));
		inv.setItem(43, Main.getOperationNodeItemLibrary().getBack());

	}

	@Override
	public void Click(int slot) {
		if (slot == 43) {
			this.getItemEditor().chengeMainMenuEditor();
			return;
		}
		if (slot == 28) {
			this.listLore();
			return;
		}
		if (slot == 29) {
			this.getItemEditor().getChat();
			this.getPlayer().closeInventory();
			this.getPlayer().sendMessage("§c请输入你要添加的叙述");
			this.getPlayer().sendMessage("§c输入getLore 获得当前lore状态。");
			this.state = 1;
		}
		if (slot == 30) {
			this.getItemEditor().getChat();
			this.getPlayer().closeInventory();
			this.getPlayer().sendMessage("§c请输入你要编辑的行数。");
			this.getPlayer().sendMessage("§c输入getLore 获得当前lore状态。");
			this.state = 2;
		}
		if (slot == 31) {
			this.getItemEditor().getChat();
			this.getPlayer().closeInventory();
			this.getPlayer().sendMessage("§c请输入你要删除的行数。");
			this.getPlayer().sendMessage("§c输入getLore 获得当前lore状态。");
			this.state = 4;
		}

	}

	private void listLore() {
		if (this.getItem().getItemMeta().getLore() == null) {
			StringBuilder ss = new StringBuilder();
			ss.append("§c=====§a物品 ");
			ss.append(this.getItem().getItemMeta().getDisplayName());
			ss.append("§a没有lore。§c======");
			this.getPlayer().sendMessage(ss.toString());
			this.getPlayer().sendMessage("§a输入 §c/ItemEditor openInv §a返回上一界面");
			return;
		}

		if (this.getItem().getItemMeta().getLore().isEmpty()) {
			StringBuilder ss = new StringBuilder();
			ss.append("§c=====§a物品 ");
			ss.append(this.getItem().getItemMeta().getDisplayName());
			ss.append("§a没有lore。§c======");
			this.getPlayer().sendMessage(ss.toString());
			this.getPlayer().sendMessage("§a输入 §c/ItemEditor openInv §a返回上一界面");
		}
		StringBuilder ss = new StringBuilder();
		ss.append("§c=====§a物品 ");
		ss.append(this.getItem().getItemMeta().getDisplayName());
		ss.append("§a的lore§c======");
		this.getPlayer().sendMessage(ss.toString());

		int i = 0;
		for (String line : this.getItem().getItemMeta().getLore()) {

			this.getPlayer().sendMessage(new StringBuilder("§c第 §a" + i + " §c行 - " + line).toString());
			i++;
		}
		this.getPlayer().sendMessage("§a输入 §c/ItemEditor openInv §a返回上一界面");
		return;
	}

	@Override
	public void upPage() {
	}

	@Override
	public void downPage() {
	}

	// 别问我为何这样copy paste 因为 懒。。。。。。。。。。。。。。
	@Override
	public boolean setChat(String m) {
		List<String> lore = this.getItem().getItemMeta().getLore();
		if (lore == null) {
			lore = new ArrayList<String>();
		}
		if (m.equalsIgnoreCase("getLore")) {
			this.getItemEditor().getChat();
			this.listLore();
			return true;
		}
		if (this.state == 1) {
			lore.add(m);
			ItemStack item = this.getItem();
			ItemMeta meta = item.getItemMeta();
			meta.setLore(lore);
			item.setItemMeta(meta);
			Main.getItemEditorManager().setItem(this.getPlayer(), item);
			Main.getItemEditorManager().openEditorInventory(this.getPlayer());
			this.getPlayer().sendMessage("§c添加成功");
			this.state = 0;
			return true;
		}
		if (this.state == 2) {
			int i = 0;
			try {
				i = Integer.valueOf(m);
			} catch (Exception e) {
				this.getPlayer().sendMessage("§c错误，你输入的不是一个数字。");
				return true;
			}
			if (i < 0 || i > lore.size()) {
				this.getPlayer().sendMessage("§c错误，指定的行数不存在。");
				return true;
			}
			this.temp = i;
			this.getPlayer().sendMessage("§c请输入你要替换成的内容。");
			this.getPlayer().sendMessage("§c输入getLore 获得当前lore状态。");
			this.getItemEditor().getChat();
			this.state = 3;
			return true;
		}
		if (this.state == 3) {
			lore.set(temp, m);
			ItemStack item = this.getItem();
			ItemMeta meta = item.getItemMeta();
			meta.setLore(lore);
			item.setItemMeta(meta);
			Main.getItemEditorManager().setItem(this.getPlayer(), item);
			Main.getItemEditorManager().openEditorInventory(this.getPlayer());
			this.getPlayer().sendMessage("§c编辑成功。");
			this.temp = 0;
			this.state = 0;
			return true;
		}
		if (this.state == 4) {
			int i = 0;
			try {
				i = Integer.valueOf(m);
			} catch (Exception e) {
				this.getPlayer().sendMessage("§c错误，你输入的不是一个数字。");
				return true;
			}
			if (i < 0 || i > lore.size()) {
				this.getPlayer().sendMessage("§c错误，指定的行数不存在。");
				return true;
			}
			lore.remove(i);
			ItemStack item = this.getItem();
			ItemMeta meta = item.getItemMeta();
			meta.setLore(lore);
			item.setItemMeta(meta);
			Main.getItemEditorManager().setItem(this.getPlayer(), item);
			Main.getItemEditorManager().openEditorInventory(this.getPlayer());
			this.getPlayer().sendMessage("§c删除成功");
			this.state = 0;
			return true;
		}

		return true;
	}

}
